package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.dao.AttrAttrgroupRelationDao;
import com.atguigu.gmall.pms.dao.AttrDao;
import com.atguigu.gmall.pms.dao.ProductAttrValueDao;
import com.atguigu.gmall.pms.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.ProductAttrValueEntity;
import com.atguigu.gmall.pms.vo.AttrGroupVO;
import com.atguigu.gmall.pms.vo.ItemGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.core.bean.PageVo;
import com.atguigu.core.bean.Query;
import com.atguigu.core.bean.QueryCondition;

import com.atguigu.gmall.pms.dao.AttrGroupDao;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Autowired
    private AttrDao attrDao;

    /**
     * 商品详情需要的
     */
    @Autowired
    private ProductAttrValueDao productAttrValueDao;

    @Override
    public PageVo queryPage(QueryCondition params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageVo(page);
    }

    /**
     * 根据组id查询组下所有的属性
     * @param gid
     * @return
     */
    @Override
    public AttrGroupVO queryById(Long gid) {

        AttrGroupVO groupVO=new AttrGroupVO();
        //1、查询分组,此处可以使用this,是因为在AttrGroup的实现类中，其他的类就不可以使用
        AttrGroupEntity groupEntity = this.getById(gid);
        BeanUtils.copyProperties(groupEntity,groupVO);
        //2、查询分组下的关联关系，必须注入关联关系的dao
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities =
                this.relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", gid));
        //判断是否为空，如果为空，直接返回组，不然需要加到组里面
        if(CollectionUtils.isEmpty(attrAttrgroupRelationEntities)){
            return groupVO;
        }
        groupVO.setRelations(attrAttrgroupRelationEntities);
        //3、查询关联关系表中，的属性id
        // 收集分组下的所有规格id,转换为新的List

        List<Long> attrIds = attrAttrgroupRelationEntities.stream()
                .map(attrAttrgroupRelationEntity -> attrAttrgroupRelationEntity.getAttrId()).collect(Collectors.toList());
        // 查询分组下的所有规格参数
        List<AttrEntity> attrEntities = this.attrDao.selectBatchIds(attrIds);
        //设置进groupVo
        groupVO.setAttrEntities(attrEntities);
        return groupVO;
    }



    /**
     * 根据三级分类id查询属性
     * @param cid
     * @param condition
     * @return
     */
    @Override
    public PageVo queryByCidPage(Long cid, QueryCondition condition) {
        QueryWrapper<AttrGroupEntity> wrapper=new QueryWrapper<>();
        if(cid!=null){
            //如果不为空，则进行条件拼接
            wrapper.eq("catelog_id",cid);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(condition),
                wrapper
        );

        return new PageVo(page);
    }

    /**
     * 先根据Cid在分组查询出组id，在根据组id在中间表查询出属性id,在根据属性id在属性表中查询出响应的属性
     * @param cid
     * @return
     */
    @Override
    public List<AttrGroupVO> queryGroupWithAttrsByCid(Long cid) {

        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", cid));//只有在本类中在可以使用list
        //类型转换groupEntity------》AttrGroupVo
        return groupEntities.stream().map(groupEntity ->
            //根据分组id,查询出下面的属性值,上面有这个方法
            this.queryById(groupEntity.getAttrGroupId())).collect(Collectors.toList());
    }

    /**
     * 商品详情
     * @param cid
     * @param spuId
     * @return
     */
    @Override
    public List<ItemGroupVO> queryItemGroupVOByCidAndSpuId(Long cid, Long spuId) {
        //1\根据分类id查询属性分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", cid));
        //2\因为返回的是ItemGroupVO,所以需要转换
        return attrGroupEntities.stream().map(group -> {
            ItemGroupVO itemGroupVO = new ItemGroupVO();

            itemGroupVO.setName(group.getAttrGroupName());//设置属性分组名称

            //2、查询规格参数及值
            List<AttrAttrgroupRelationEntity> attrgroupRelationEntities = this.relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id",group.getAttrGroupId()));//中间表
            if(attrgroupRelationEntities.size()>0){
                List<Long> attrIds = attrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());//获取中间表的属性id

                //3、查询规格参数值的表
                List<ProductAttrValueEntity> productAttrValueEntities = this.productAttrValueDao.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId).in("attr_id", attrIds));

                itemGroupVO.setProductAttrValueEntityList(productAttrValueEntities);
            }
            return itemGroupVO;
        }).collect(Collectors.toList());

    }
}